#!/bin/sh -
#
#	initialization/shutdown script for webcp backend server
# <http://www.web-cp.net/>
WEBCP_DIR=/usr/local/webcp
WEBCP_PIDFILE=/var/run/webcp.pid

case "$1" in
start)
	# test for running proc:
	if test -r ${WEBCP_PIDFILE}
	then
		pid=`cat ${WEBCP_PIDFILE}`
		# test if process is running:
		if `kill -CHLD ${pid} > /dev/null 2>&1`
		then
			echo "$0[$$] process already running, cowardly refusing to start new process."
			exit 0
		fi

		# if we got here, pid file is stale:
		echo "Stale ${WEBCP_PIDFILE}, deleting..."
		rm -f ${WEBCP_PIDFILE}
	fi

	# start the server:
	cd ${WEBCP_DIR}/server
	./webcp.php -d >/dev/null 2>&1 && echo -n ' webcp'
	;;
stop)
	kill `cat $WEBCP_PIDFILE` && echo -n ' webcp'
	rm -f ${WEBCP_PIDFILE}
	;;
*)
	echo "unknown option: $1 - should be 'start' or 'stop'" >&2
	;;
esac
